package manyAnimal;

import java.util.Scanner;

public class Animaltest {
    static Scanner input = new Scanner(System.in);
    static Animal[] animal = new Animal[3];

    static {
        animal[0] = new Cat("加菲", 4);

        animal[1] = new Dolphin("奇奇", 0);

        animal[2] = new Duck("糖糖", 2);
    }

    public static void show()
    {
        for(Animal animal:animal)
        {
            animal.show();
        }
    }

    public static void main(String[] args) {
        int n;
        do{
            n=-1;
            show();
            System.out.println("修改吗?0继续");
            n=input.nextInt();
            if(n==0)
            {
                revision();
            }

        }while (n==0);
    }
    public static void revision() {

        for (Animal animal : animal) {
            if (animal instanceof Cat) {
                Cat cat = (Cat) animal;
                System.out.println("请输入猫咪的名称:");
                String name = input.next();
                System.out.println("请输入猫咪的腿数量:");
                int newLeg = input.nextInt();
                int olgLeg = cat.getLegNum();
                try {
                    if (newLeg != olgLeg) {
                        throw new illegalLegNumException("喵咪的腿只能是4条!!");
                    }
                    cat.setName(name);
                } catch (illegalLegNumException e) {
                    e.printStackTrace();
                    break;
                }
            } else if (animal instanceof Dolphin) {
                Dolphin dolphin = (Dolphin) animal;
                System.out.println("请输入海豚的名称:");
                String name = input.next();
                System.out.println("请输入海豚的腿数量:");
                int newLeg = input.nextInt();
                int olgLeg = dolphin.getLegNum();
                try {
                    if (newLeg != olgLeg) {
                        throw new illegalLegNumException("海豚的腿只能是0条!!");
                    }
                    dolphin.setName(name);
                } catch (illegalLegNumException e) {
                    e.printStackTrace();
                    break;
                }
            } else if (animal instanceof Duck) {
                Duck duck = (Duck) animal;
                System.out.println("请输入鸭的名称:");
                String name = input.next();
                System.out.println("请输入鸭的腿数量:");
                int newLeg = input.nextInt();
                int olgLeg = duck.getLegNum();
                try {
                    if (newLeg != olgLeg) {
                        throw new illegalLegNumException("鸭的腿只能是2条!!");
                    }
                    duck.setName(name);
                } catch (illegalLegNumException e) {
                    e.printStackTrace();
                    break;
                }
            }


        }
    }
}